var apiUrl = "http://site.87money.com/money/API_UNITE/";
var fundCode = 630011;
var fundName = "华商主题精选股票"

	var $curvType = $("#js_curv_type li");
	var $curvMonth = $("#js_curv_month dd");
	var $curv = $("#js_curv_worth");
	
	function getTypeActive(){
		return $curvType.parent().find(".active").data("type");
	};
	function getMonthActive(){
		return $curvMonth.parent().find(".active").data("month");
		
	};
	function changeData(){
		var getType = getTypeActive();
		var getMonth = getMonthActive();
		console.log(getType);
		console.log(getMonth);
		getData(getType, getMonth, fundCode);
	}
	$curvType.click(function(){
		$curvType.removeClass("active");
		$(this).addClass("active");
		changeData()
	});
	$curvMonth.click(function(){
		$curvMonth.removeClass("active");
		$(this).addClass("active");
		changeData();
	});
	getData(0, "year", fundCode)
	//画图函数
    //param dataSeries: array ; 数据
	function drawCurv(dataSeries){
		$curv.highcharts({
			credits: false,
			title: " ",
			chart: {
				type: "spline"
			},
			xAxis: {
				type: "datetime",
				labels: {
					formatter: function(){
						return  Highcharts.dateFormat("%m-%e",this.value);
					}
				}
			},
			yAxis: {
				title: " "	
			},
			series: dataSeries,
			plotOptions: {
				spline: {  
					shadow: false,  
					animation: false,  
					lineWidth: 1  
				},  
				series: {
					marker: {  
						enabled: false,
						states: {
							hover: {
								fillColor: "#ffffff",
								radius: 2
							}
						}
					}  
				} 
			},
			tooltip: {
				formatter: function(){
					var s = Highcharts.dateFormat("%Y-%m-%e",this.x);
					$.each(this.points, function(){
						if(this.series.name == "累计收益"){
							s += "<br>" + fundName + ":" + "<b>" + this.y + "</b>"; 
						}else{
							s += "<br>" + this.series.name + ":" + "<b>" + this.y + "</b>";
						}
					});
					
					return s;
				},
				shared: true
			}
		});
	};

   //ajax获取数据函数
   //param ctype: int; 类型0累计收益走势，1单位净值走势，2累计净值走势
   //param cmonth: string; 获取时间month一个月,threemonth三个月，sixmonth六个月，year一年，threeyear三年，thisyear今年以来，all最大
   //param ccode： int；基金代码
   function getData(ctype, cmonth, ccode){
	$.ajax({
		url: apiUrl+"getfundmove",
		type: "post",
		data: {
			"code": ccode,
			"dt": cmonth,
			"type": ctype
		},
		dataType: "jsonp",
		success: function(data){
			console.log(data);
			var ljsyData=[],hsData=[],szData=[],dwData=[],ljData=[];
			if(ctype === 0){
					$.each(data.info.ljsy, function(i){
						var subDate = [];
						subDate.push(this.date);
						subDate.push(this.val);
						ljsyData.push(subDate);
					});
					$.each(data.info.hs, function(i){
						var subDate = [];
						subDate.push(this.date);
						subDate.push(this.val);
						hsData.push(subDate);
					});
					$.each(data.info.sz, function(i){
						var subDate = [];
						subDate.push(this.date);
						subDate.push(this.val);
						szData.push(subDate);
					});


				var dataSeries = [
					{
						name: "累计收益",
						data: ljsyData,
						color: "#0099cb"
					},
					{
						name: "沪深300",
						data: hsData,
						color: "#fe0000"
					},
					{
						name: "上证指数",
						data: szData,
						color: "#ff9900"
					}
				];
			}else if(ctype === 1){
				$.each(data.info.ljsy, function(i){
						var subDate = [];
						subDate.push(this.date);
						subDate.push(this.val);
						ljsyData.push(subDate);
					});
				var dataSeries = [
					{
						name: "单位净值",
						data: ljsyData,
						color: "#0099cb"
					}
				]
			}else {
				$.each(data.info.ljsy, function(i){
						var subDate = [];
						subDate.push(this.date);
						subDate.push(this.val);
						ljsyData.push(subDate);
					});
				var dataSeries = [
					{
						name: "累计净值",
						data: ljsyData,
						color: "#0099cb"
					}
				]
			}
			drawCurv(dataSeries);
		}
	});
  };

	
	